import 'package:paly_android/generated/json/base/json_convert_content.dart';

class HttpResult<T> {
  late int? code;
  late String msg;
  T? data;

  HttpResult(this.code, this.msg, this.data);

  HttpResult.formJson(Map<String, dynamic> json) {
    code = json["errorCode"];
    msg = json["errorMsg"];
    if (isOk()&&json.containsKey("data")) {
      data = _generateOBJ<T>(json["data"]);
    }
  }

  bool isOk(){
    return 0==code;
  }

  T _generateOBJ<O>(Object json) {
    if (T.toString() == 'String') {
      return json.toString() as T;
    } else if (T.toString() == 'Map<dynamic, dynamic>') {
      return json as T;
    } else {
      /// List类型数据由fromJsonAsT判断处理
      // List<dynamic> list=json as List<dynamic> ;
      // List<Map<String, dynamic>> newList=List.empty(growable: true);
      // list.forEach((element) {
      //   var data=element as Map<String,dynamic>;
      //   newList.add(data);
      // });
      // return newList as T;
      return JsonConvert.fromJsonAsT(json);
    }
  }
}
